package io.ent.modules.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.ent.common.utils.PageUtils;
import io.ent.common.utils.Query;
import io.ent.modules.product.dao.ProductCasePicsDao;
import io.ent.modules.product.dao.ProductInfoDao;
import io.ent.modules.product.entity.ProductInfoEntity;
import io.ent.modules.product.po.ProductCaseInfoPicEntityIfPo;
import io.ent.modules.product.po.ProductInfoEntityIfPo;
import io.ent.modules.product.po.ProductInfoEntityTypePo;
import io.ent.modules.product.service.ProductInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("productInfoService")
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoDao, ProductInfoEntity> implements ProductInfoService {
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private ProductCasePicsDao productCasePicsDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        String typeId = (String)params.get("typeId");
        Integer newTypeId = null;
        if (StringUtils.isBlank(name)){
            name=null;
        }

        if (typeId==null || typeId.equals("0")){
            newTypeId=null;
        }else {
            newTypeId=Integer.parseInt(typeId);
        }
        IPage<ProductInfoEntityTypePo> page1 = new Query<ProductInfoEntityTypePo>().getPage(params);
       /* IPage<ProductInfoEntity> page = this.page(
                new Query<ProductInfoEntity>().getPage(params),
                new QueryWrapper<ProductInfoEntity>()
        );*/
        Page<ProductInfoEntityTypePo> page2 = new Page<ProductInfoEntityTypePo>(page1.getCurrent(),page1.getSize());
        IPage<ProductInfoEntityTypePo> allProductInfo = productInfoDao.getAllProductInfo(page2,name,newTypeId);

        return new PageUtils(allProductInfo);
    }



    //查找所有的产品信息---公开的接口---分页
    @Override
    public PageUtils getProductInfos(Map<String, Object> params) {
        IPage<ProductInfoEntityIfPo> page1 = new Query<ProductInfoEntityIfPo>().getPage(params);
        Page<ProductInfoEntityIfPo> page = new Page<ProductInfoEntityIfPo>(
                page1.getCurrent(),
                page1.getSize());
        IPage<ProductInfoEntityIfPo> productInfos = productInfoDao.getProductInfos(page);
        for (ProductInfoEntityIfPo info:productInfos.getRecords()) {
            List<ProductCaseInfoPicEntityIfPo> pics = productCasePicsDao.getPics(info.getId(), 1);
            info.setPicList(pics);
        }
        return new PageUtils(productInfos);
    }

    //根据产品类型id查找相同的产品信息
    @Override
    public PageUtils getProductInfosByTypeId(Integer typeId,Long pageNo,Long limit) {
        Page<ProductInfoEntityIfPo> page = new Page<ProductInfoEntityIfPo>(
                pageNo,
                limit);
        IPage<ProductInfoEntityIfPo> productInfos = productInfoDao.getProductInfosByTypeId(page,typeId);
        for (ProductInfoEntityIfPo info:productInfos.getRecords()) {
            List<ProductCaseInfoPicEntityIfPo> pics = productCasePicsDao.getPics(info.getId(), 1);
            info.setPicList(pics);
        }
        return new PageUtils(productInfos);
    }

}
